package com.mall.order.rocketmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.promo.dto.OrderPromoProductRequest;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author xudengfeng
 * @date 2020/12/26 9:44
 */
public class PromoOrderMqConsumer {

    private DefaultMQPushConsumer mqPushConsumer;

    @PostConstruct
    public void init() {
        mqPushConsumer = new DefaultMQPushConsumer("promo_order_consumer");
        mqPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            mqPushConsumer.subscribe("promo_order", "*");
        } catch (MQClientException e) {
            e.printStackTrace();
        }

        mqPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);
                byte[] body = messageExt.getBody();
                String message = new String(body);

                OrderPromoProductRequest request = JSON.parseObject(message, OrderPromoProductRequest.class);
                
                return null;
            }
        });
    }
}
